﻿using System;
using System.Collections.Generic;
using QuantBox.XApi;

namespace QuantBox
{
    internal class RealtimeTickReader
    {
        public static DepthMarketDataField[] Read(string symbol, object[][] data)
        {
            var ticks = new List<DepthMarketDataField>(data.Length);
            var last = (0, 0d, 0);
            foreach (var item in data) {
                if (item.Length != 9)
                    continue;
                var tick = new DepthMarketDataField();
                tick.Asks = new DepthField[1];
                tick.Bids = new DepthField[1];
                tick.Symbol = symbol;
                tick.SetExchangeDateTime(DateTime.ParseExact(item[0].ToString(), "yyyy-MM-dd HH:mm:ss.fff", null));
                tick.LastPrice = double.Parse(item[1].ToString());
                var temp = (int.Parse(item[2].ToString()),
                    double.Parse(item[3].ToString()),
                    int.Parse(item[4].ToString()));
                tick.Volume = temp.Item1 - last.Item1;
                tick.Turnover = temp.Item2 - last.Item2;
                tick.OpenInterest = temp.Item3 - last.Item3;
                tick.Asks[0].Price = double.Parse(item[5].ToString());
                tick.Asks[0].Size = int.Parse(item[6].ToString());
                tick.Bids[0].Price = double.Parse(item[7].ToString());
                tick.Bids[0].Size = int.Parse(item[8].ToString());
                ticks.Add(tick);
                last = temp;
            }
            return ticks.ToArray();
        }
    }
}